/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_TESTDRIVER_TEST_CASE_H_INCLUDED_
#define ALKES_TESTDRIVER_TEST_CASE_H_INCLUDED_

#include "alkes.h"
#include "alkes/tree.h"
#include "alkes/testdriver/internal.h"
#include "alkes/testdriver/String.h"

#define AL_TC_ASSERT(exp)               alkes::TestCase::assertion(exp, #exp, __FILE__, __LINE__, __FUNC_NAME__)
#define AL_TC_EQUALS(lh, rh)            alkes::TestCase::equals(lh, rh, __FILE__, __LINE__, __FUNC_NAME__)
#define AL_TC_EQUALS_DELTA(lh, rh, delta) alkes::TestCase::equals_delta(lh, rh, delta, __FILE__, __LINE__, __FUNC_NAME__)
#define AL_TC_DIFFERS(lh, rh)           alkes::TestCase::differs(lh, rh, __FILE__, __LINE__, __FUNC_NAME__)
#define AL_TC_LESS_THAN(lh, rh)         alkes::TestCase::less_than(lh, rh, __FILE__, __LINE__, __FUNC_NAME__)
#define AL_TC_LESS_THAN_EQUAL(lh, rh)   alkes::TestCase::less_than_equal(lh, rh, __FILE__, __LINE__, __FUNC_NAME__)
#if !defined(__GNUC__)
#define AL_TC_LOG(fmt, ...)             alkes_dprintf(AL_ERRLV_NOTICE, fmt "\n", __VA_ARGS__)
#else
#define AL_TC_LOG(fmt, ...)             alkes_dprintf(AL_ERRLV_NOTICE, fmt "\n", ## __VA_ARGS__)
#endif

namespace alkes {

template<class T>
struct same_type { typedef T type; };

class AL_TD_EXPORT TestCase
{
public:
    static void assertion(bool exp, const char* exp_str, const char* filename, int lineno, const char* funcname);

#define DEFINE_INT_ASSERTION(type)  \
    static void assertion(type exp, const char* exp_str, const char* filename, int lineno, const char* funcname) \
    {\
        assertion(0 != exp, exp_str, filename, lineno, funcname);\
    }

    DEFINE_INT_ASSERTION(int8_t)
    DEFINE_INT_ASSERTION(uint8_t)
    DEFINE_INT_ASSERTION(int16_t)
    DEFINE_INT_ASSERTION(uint16_t)
    DEFINE_INT_ASSERTION(int32_t)
    DEFINE_INT_ASSERTION(uint32_t)
    DEFINE_INT_ASSERTION(int64_t)
    DEFINE_INT_ASSERTION(uint64_t)
    DEFINE_INT_ASSERTION(const void*)

    template<class T>
    static void equals(
        const T expected, const typename same_type<T>::type actual,
        const char* filename, int lineno, const char* funcname);

    template<class T>
    static void equals_delta(
        const T expected, const typename same_type<T>::type actual, const typename same_type<T>::type delta,
        const char* filename, int lineno, const char* funcname);

    template<class T>
    static void differs(
        const T lh, const typename same_type<T>::type rh,
        const char* filename, int lineno, const char* funcname);

    template<class T>
    static void less_than(
        const T lh, const typename same_type<T>::type rh,
        const char* filename, int lineno, const char* funcname);

    template<class T>
    static void less_than_equal(
        const T lh, const typename same_type<T>::type rh,
        const char* filename, int lineno, const char* funcname);

protected:
    static void equals_failed(const char* expected, const char* actual, const char* filename, int lineno, const char* funcname);
    static void equals_delta_failed(const char* expected, const char* actual, const char* delta, const char* filename, int lineno, const char* funcname);
    static void differs_failed(const char* lh, const char* rh, const char* filename, int lineno, const char* funcname);
    static void less_than_failed(const char* lh, const char* rh, const char* filename, int lineno, const char* funcname);
    static void less_than_equal_failed(const char* lh, const char* rh, const char* filename, int lineno, const char* funcname);
};


template<class T>
void TestCase::equals(
    const T expected, const typename same_type<T>::type actual,
    const char* filename, int lineno, const char* funcname)
{
    if (!(expected == actual))
    {
        String lh_str(expected);
        String rh_str(actual);
        equals_failed(lh_str.c_str(), rh_str.c_str(), filename, lineno, funcname);
    }
}

template<class T>
void TestCase::equals_delta(
    const T expected, const typename same_type<T>::type actual, const typename same_type<T>::type delta,
    const char* filename, int lineno, const char* funcname)
{
    T d = expected < actual ? actual - expected : expected - actual;
    if (!(d <= delta))
    {
        String lh_str(expected);
        String rh_str(actual);
        String delta_str(delta);
        equals_delta_failed(lh_str.c_str(), rh_str.c_str(), delta_str.c_str(), filename, lineno, funcname);
    }
}

template<class T>
void TestCase::differs(
    const T lh, const typename same_type<T>::type rh,
    const char* filename, int lineno, const char* funcname)
{
    if (!(lh != rh))
    {
        String lh_str(lh);
        String rh_str(rh);
        differs_failed(lh_str.c_str(), rh_str.c_str(), filename, lineno, funcname);
    }
}

template<class T>
void TestCase::less_than(
    const T lh, const typename same_type<T>::type rh,
    const char* filename, int lineno, const char* funcname)
{
    if (!(lh < rh))
    {
        String lh_str(lh);
        String rh_str(rh);
        less_than_failed(lh_str.c_str(), rh_str.c_str(), filename, lineno, funcname);
    }
}

template<class T>
void TestCase::less_than_equal(
    const T lh, const typename same_type<T>::type rh,
    const char* filename, int lineno, const char* funcname)
{
    if (!(lh <= rh))
    {
        String lh_str(lh);
        String rh_str(rh);
        less_than_equal_failed(lh_str.c_str(), rh_str.c_str(), filename, lineno, funcname);
    }
}

}

#endif
